<?php namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;

class ApplyAgent extends CxhshopBase {
    protected $table = 'ims_cxh_apply_agent';
    protected $fillable = [];
    public $timestamps = false;
    /**
     * 申请代理商
     * @param $memId
     * @param $params
     * @return int
     */
    public function addAgent($uuid,$memId,$params){
        $data = [
            'member_uuid'=>$uuid,
            'member_id'=>$memId,
            'aa_agent'=>$params['agent_level'],
            'aa_provinceName'=>$params['province'],
            'aa_cityNname'=>$params['city'],
            'aa_areaName'=>$params['area'],
            'aa_contractImg'=>$params['contract_img'],
            'ctime'=>time(),
            'utime'=>time(),
        ];
        $query = self::query();
        $applyAgentId = $query -> insertGetId($data);
        return $applyAgentId;
    }

    /**
     * @param int $memId
     * @param int $type 2：判断是否有代理商申请记录记录（无论是待审核，已拒绝，还是已经通过）；1判断是否有申请成功的记录
     * @return array
     * 判断当前用户是否申请过代理商（申请成功）
     */
    public function checkmemAgent($memId = 0,$type = 1){
        $query = self::query();
        if($type == 1){
            $info = $query -> where('member_id',$memId) -> whereIn('aa_status',[0,1]) -> get();
        }else{
            $info = $query -> where('member_id',$memId) -> get();
        }
        if($info){
           $data = $info -> toArray();
        }else{
            $data = [];
        }
        return $data;
    }

    /**
     * 判断当前区域是否存在代理商
     * @param $agentLevel
     * @param $province
     * @param $city
     * @param $area
     * @return array
     */
    public function checkAgentArea($agentLevel,$province,$city,$area){
        $query = self::query();
        $info = $query
            -> where('aa_agent',$agentLevel)
            -> where('aa_provinceName',$province)
            -> where('aa_cityNname',$city)
            -> where('aa_areaName',$area)
            -> where('aa_status',1)
            -> get();
        if($info){
            $data = $info -> toArray();
        }else{
            $data = [];
        }
        return $data;
    }

    /**
     * 获取代理商申请记录
     * @param $uuid
     * @param $applyFields
     * @return array
     */
    public function getApplyList($uuid,$applyFields){
        $query = self::query();
        $applyList = $query -> where('member_uuid',$uuid) ->orderBy('id','desc') ->get($applyFields);
        if($applyList){
            $data = $applyList -> toArray();
        }else{
            $data = [];
        }
        return $data;
    }

}